package huawei.js2025;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 小火车最多人时所在园区站点
 */
public class Question22 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < N; i++) {
            int start = in.nextInt();
            int end = in.nextInt();

            map.put(end, map.getOrDefault(end, 0) + 1);
            while (start != end) {
                map.put(start, map.getOrDefault(start, 0) + 1);

                start += 1;
                start = start == 6 ? 1 : start;
            }
        }
        List<Map.Entry<Integer, Integer>> collect = map.entrySet().stream().sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed()).collect(Collectors.toList());
        System.out.println(collect.get(0).getKey());


    }
}
